home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / xbm.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  5KB  |  177 lines

  1. /*
  2.  * $Id: xbm.c,v 0.91 1994/02/20 00:52:53 zhao Pre-Release $
  3.  *
  4.  *. This file is part of BIT shareware package. After the two weeks of
  5.  *  free evaluation period, you are encouraged (required) to register
  6.  *  your copy for a small registration fee, which is $35 for personal use
  7.  *  and $50 for commercial, government and institutional use.
  8.  *
  9.  *  Copyright(c) 1993, 1994 by T.C. Zhao.
  10.  *  All rights reserved.
  11.  *
  12.  *  Permission to use, copy, and distribute this software in its entirety
  13.  *  for non-commercial purposes is hereby granted, provided that the
  14.  *  above shareware and copyright notices and this permission notice
  15.  *  appear in all copies and their documentation.
  16.  *
  17.  *  This software may be modified for your own use, but modified versions
  18.  *  may not be distributed without prior consent of the author.
  19.  *
  20.  *  This software is provided "as is" without expressed or implied
  21.  *  warranty of any kind.
  22.  *
  23.  *.
  24.  *
  25.  * X Window bitmaps
  26.  */
  27. #ifndef NO_XBM
  28.  
  29. #if !defined(lint) && defined(F_ID)
  30. char *id_xbm = "$Id: xbm.c,v 0.91 1994/02/20 00:52:53 zhao Pre-Release $";
  31. #endif
  32.  
  33. #include "bit.h"
  34. extern const char *hexdigits;
  35.  
  36.  
  37. int
  38. XBM_desc(IPTR im)
  39. {
  40.     char tmpstr[256];
  41.     int w = -1, h = -1, no_size = 1, c;
  42.  
  43.     while (no_size && fgets(tmpstr, sizeof(tmpstr), im->fp))
  44.       {
  45.       if (sscanf(tmpstr, "#define %*s %d", &c) == 1)
  46.         {
  47.         if (strstr(tmpstr, "_width"))
  48.             w = c;
  49.         else if (strstr(tmpstr, "_height"))
  50.             h = c;
  51.         no_size = w < 1 || h < 1;
  52.         }
  53.       }
  54.  
  55.     im->w = w;
  56.     im->h = h;
  57.     im->colors = im->cmap->colors = 2;
  58.     for (w = 0; w < 3; w++)
  59.       {
  60.       im->cmap->ct[w][0] = PCMAXV;
  61.       im->cmap->ct[w][1] = 0;
  62.       }
  63.  
  64.     /* skip until we get brace */
  65.     while ((h = getc(im->fp)) != EOF && h != '{')    /* } VI */
  66.     ;
  67.  
  68.     return h == EOF ? EOF : 0;
  69. }
  70.  
  71. int
  72. XBM_load(IPTR im)
  73. {
  74.     register int c, ct, i, j, err;
  75.     register ci_t *bits;
  76.     long rlines;
  77.  
  78.     rlines = progress_report("Loading XBM ...", im->h);
  79.     for (j = err = c = 0; j < im->h && !err; j++)
  80.       {
  81.       bits = ((ci_t **) im->mraster)[im->h - 1 - j];
  82.       REPORT(j, rlines);
  83.       for (i = ct = 0; i < im->w && !err; i++, ct = (++ct) & 7)
  84.         {
  85.         if (!ct)
  86.             err = (c = readhexint(im->fp)) < 0;
  87.         *bits++ = (c & 1);
  88.         c = c >> 1;
  89.         }
  90.       }
  91.     if (err)
  92.       {
  93.       Bark("XBM_load", "Junk in hex stream");
  94.       }
  95.     remove_progress_report();
  96.     return (j > im->h / 2) ? j : -1;
  97. }
  98.  
  99. #define write0xhex(fp, c)                                        \
  100.    do {                                                          \
  101.      putc('0', fp);                                              \
  102.      putc('x', fp);                                              \
  103.      putc(hexdigits[(c) >> 4], fp);                              \
  104.      putc(hexdigits[(c) & 15], fp);                              \
  105.    } while(ZERO)
  106.  
  107. #define Newline                                                  \
  108.    do {                                                          \
  109.       if ((len += 5)> 70)                                        \
  110.       {                                                          \
  111.                putc('\n', fp);                                   \
  112.                putc(' ', fp);                                    \
  113.                len = 1;                                          \
  114.       }                                                          \
  115.    } while (ZERO)
  116.  
  117. #include <ctype.h>
  118.  
  119. int
  120. XBM_dump(IPTR im)
  121. {
  122.     char tmpstr[256], *p;
  123.     ci_t *bits;
  124.     int nbits, k, len, i, j;
  125.     FILE *fp = im->fp;
  126.     long rlines;
  127.  
  128.     Strncpy(tmpstr, im->ofile, sizeof(tmpstr) - 25);
  129.     if ((p = strchr(tmpstr, '.')))
  130.     *p = '\0';
  131.  
  132.     if (isdigit(tmpstr[0]))
  133.     tmpstr[0] = 'a';
  134.  
  135.     fprintf(im->fp, "#define %s_width %d\n#define %s_height %d\n",
  136.         tmpstr, im->w, tmpstr, im->h);
  137.     fprintf(im->fp, "static char %sbits[] = {\n ", tmpstr);
  138.  
  139.     rlines = progress_report("Writing XBM ...", im->h);
  140.     for (j = 0, len = 1; j < im->h; j++)
  141.       {
  142.       REPORT(j, rlines);
  143.       bits = ((ci_t **) im->mraster)[im->h - 1 - j];
  144.       for (i = nbits = k = 0; i < im->w; i++, bits++)
  145.         {
  146.         k = k >> 1;
  147.         if (*bits)
  148.             k |= 0x80;
  149.         if (++nbits == 8)
  150.           {
  151.               k &= 255;
  152.               write0xhex(fp, k);
  153.               if (!((j == (im->h - 1)) && (i == (im->w - 1))))
  154.               putc(',', fp);
  155.               Newline;
  156.               nbits = k = 0;
  157.           }
  158.         }
  159.  
  160.       /* check for possible padding */
  161.       if (nbits)
  162.         {
  163.         k = k >> (8 - nbits);
  164.         k &= 255;
  165.         write0xhex(fp, k);
  166.         if (j != im->h - 1)
  167.             putc(',', fp);
  168.         Newline;
  169.         }
  170.       }
  171.     fputs("};\n", fp);
  172.     remove_progress_report();
  173.     return fflush(fp);
  174. }
  175.  
  176. #endif
  177.